home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 6067 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  61 lines

  1. Path: cs.ruu.nl!usenet
  2. From: wsldanke@cs.ruu.nl (Wessel Dankers)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: Random #s
  5. Date: 23 Mar 96 02:20:12 +0100
  6. Organization: Dept of Computer Science, Utrecht University, The Netherlands
  7. Message-ID: <1496.6656T140T637@cs.ruu.nl>
  8. References: <4ihkif$ed0@apollo.isisnet.com> <4irnj8$a2t@dunlop.cs.strath.ac.uk>
  9. NNTP-Posting-Host: anx1p16.cc.ruu.nl
  10. X-Newsreader: THOR 2.22 (Amiga TCP/IP)
  11.  
  12. Paul Callaghan <pcallagh@cs.strath.ac.uk> wrote:
  13. > Here is a little program which will generate random numbers
  14. > (Off the top of my head so it `should` work.)
  15. > (rand() % MUTATE)+
  16. > #include <time.h>
  17. > #define HI 6
  18. > #define LO 1
  19.  
  20.  
  21. > void main()
  22. > {
  23. >  int rand_no;
  24.  
  25. >  srand(time(NULL));
  26.  
  27. >  rand_no=(rand() % HI)+LO;
  28. > }
  29.  
  30. One of the most commonly used (but not one of the best) methods of generating
  31. pseudorandom numbers is the linear congruential method, which works as
  32. follows. Let x_0 be an initial number (the "seed")> The sequence is generated
  33. recursively as
  34.  
  35.           x_n = (ax_n-1 + c) mod m
  36.  
  37. Making good choises of a, c and m involves both art and theory. The following
  38. are some values that have been proposed:
  39. (1) a = 69069, c = 0, m = 2^31
  40. (2) a = 65539, c = 0, m = 2^31
  41. The latter is an infamous generator called RANDU.
  42.  
  43. AFAIK a better one is:
  44.  
  45. /* returns a random number x | a <= x < b */
  46. unsigned int my_rand(void)
  47. { static unsigned int seed = 0x47E25FC4;
  48.   return seed = (seed >> 1) | ((((seed&1)^((seed & 2)>>1)))<<31);
  49. }
  50.  
  51. I hope there are no pesky sequence points involved this time :-)
  52.  
  53. --
  54. Wessel Dankers                 _\\|//_            <wsldanke@cs.ruu.nl>
  55.                                ///|\\\
  56. ----------------------------oOO--(_)---OOo----------------------------
  57. `Never imagine yourself not to be otherwise than what it might appear
  58. to others that what you were or might have been was not otherwise than
  59. what you had been would have appeared to them to be otherwise.'
  60.  
  61.